let Methods = require('./methods');

class Cell{
    constructor(i,j,boardInfo){
        this.rowIdx = i;
        this.colIdx = j;
        this.arr = [];

        this.boardInfo = boardInfo;
        let board = this.board = boardInfo.board;
        let boardSize = board.length;
        for(let i = 1; i <= boardSize; ++i){
            this.arr.push(i);
        }

        this.gongIdx = boardInfo.calGongIdx(this.rowIdx,this.colIdx);
    }

    simplify(){
        // 同一行中出现的数
        let set = new Set();
        for(let j = 0; j < this.board[0].length;++j){
            let value = this.board[this.rowIdx][j];
            if(value > 0){
                set.add(value);
            }
        }

        // 同一列中出现的数
        for(let i = 0; i < this.board.length;++i){
            let value = this.board[i][this.colIdx];
            if(value > 0){
                set.add(value);
            }
        }

        // 同一宫中出现的数
        // 计算当前在哪一宫
        let gongRowCount = this.boardInfo.gongRowCount;
        let gongColCount = this.boardInfo.gongColCount;
        let ids = this.boardInfo.getGongStartIdx(this.gongIdx);
        for(let i = 0; i < gongRowCount; ++i){
            for(let j = 0; j < gongColCount; ++j){
                let ii = ids.x + i;
                let jj = ids.y + j;
                let value = this.board[ii][jj];
                if(value > 0){
                    set.add(value);
                }
            }
        }

        //  删除
        set.forEach(value=>{
            let idx = this.arr.indexOf(value);
            if(idx >= 0){
                this.arr.splice(idx,1);
            }
        });
    }

    getMaybeArr(){
        return this.arr;
    }

    getResult(){
        if(this.arr.length === 1){
            return {
                x:this.rowIdx,
                y:this.colIdx,
                type:Methods.Mod,
                value:this.arr[0]
            }
        }
        if(this.arr.length < 1){
            throw new Error(`error:地图错误????`);
        }
        return null;
    }

    getTipsObj(){
        return {
            x:this.rowIdx,
            y:this.colIdx,
            arr:this.getMaybeArr(),
        }
    }
}


module.exports = Cell;
